package cn.edu.ujn.project.supermarket.controller;

import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import cn.edu.ujn.project.supermarket.domain.DealReport;
import cn.edu.ujn.project.supermarket.domain.GoodsInfor;
import cn.edu.ujn.project.supermarket.domain.Supplier;
import cn.edu.ujn.project.supermarket.extendbean.PurchaseReportSon;
import cn.edu.ujn.project.supermarket.service.IDealReportService;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import cn.edu.ujn.project.common.annotation.Log;
import cn.edu.ujn.project.common.core.controller.BaseController;
import cn.edu.ujn.project.common.core.domain.AjaxResult;
import cn.edu.ujn.project.common.core.page.TableDataInfo;
import cn.edu.ujn.project.common.enums.BusinessType;
import cn.edu.ujn.project.common.utils.poi.ExcelUtil;
import cn.edu.ujn.project.supermarket.domain.PurchaseReport;
import cn.edu.ujn.project.supermarket.extendbean.PurchaseReportChild;
import cn.edu.ujn.project.supermarket.service.IGoodsInforService;
import cn.edu.ujn.project.supermarket.service.IPurchaseReportService;
import cn.edu.ujn.project.supermarket.service.ISupplierService;
import cn.edu.ujn.project.system.service.ISysUserService;
import tool.Generator;

/**
 * 报表Controller
 *
 * @author fenglc
 * @date 2023-11-14
 */
@Controller
@RequestMapping("/system/purchasereport")
public class PurchaseReportController extends BaseController {
    private String prefix = "system/purchasereport";

    @Autowired
    private IPurchaseReportService purchaseReportService;

    @Autowired
    private IDealReportService dealReportService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IGoodsInforService goodsInforService;

    @Autowired
    private ISupplierService supplierService;

    @RequiresPermissions("system:purchasereport:view")
    @GetMapping()
    public String purchasereport() {
        return prefix + "/purchasereport";
    }

    /**
     * 查询报表列表
     */
    @RequiresPermissions("system:purchasereport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PurchaseReport purchaseReport) {
        startPage();
        List<PurchaseReport> list = purchaseReportService.selectPurchaseReportList(purchaseReport);
        List<PurchaseReportChild> list2 = new ArrayList<PurchaseReportChild>();
        for (PurchaseReport purchase : list) {
            String userName = sysUserService.selectUserById(Long.valueOf(purchase.getApplication())).getUserName();
            String name = goodsInforService.selectGoodsInforByBarCode(purchase.getBarCode()).getName();
            String supplierName = supplierService.selectSupplierBySupplierId(String.valueOf(purchase.getSupplierId())).getName();

            list2.add(new PurchaseReportChild(userName, name, supplierName, purchase));
        }
        return getDataTable(list2);
    }

    /**
     * 导出报表列表
     */
    @RequiresPermissions("system:purchasereport:export")
    @Log(title = "报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PurchaseReport purchaseReport) {
        List<PurchaseReport> list = purchaseReportService.selectPurchaseReportList(purchaseReport);
        List<PurchaseReportChild> list2 = new ArrayList<PurchaseReportChild>();
        for (PurchaseReport purchase : list) {
            String userName = sysUserService.selectUserById(Long.valueOf(purchase.getApplication())).getUserName();
            String name = goodsInforService.selectGoodsInforByBarCode(purchase.getBarCode()).getName();
            String supplierName = supplierService.selectSupplierBySupplierId(String.valueOf(purchase.getSupplierId())).getName();
            list2.add(new PurchaseReportChild(userName, name, supplierName, purchase));
        }
        ExcelUtil<PurchaseReportChild> util = new ExcelUtil<PurchaseReportChild>(PurchaseReportChild.class);
        return util.exportExcel(list2, "报表数据");
    }

    /**
     * 新增报表
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存报表
     */
    @RequiresPermissions("system:purchasereport:add")
    @Log(title = "报表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public TableDataInfo addSave(PurchaseReport purchaseReport) {
        List<GoodsInfor> goodsInfors = this.goodsInforService.selectGoodsInforList(new GoodsInfor());
        List<Supplier> suppliers = this.supplierService.selectSupplierList(new Supplier());
        Integer quantity = 0;
        List<PurchaseReportSon> list = new ArrayList<>();
        list.add(new PurchaseReportSon(goodsInfors, suppliers, quantity));
        return getDataTable(list);
    }

    @PostMapping("/addpurchaseitem")
    @ResponseBody
    public String addPurchaseItem(@RequestBody String json) {
        HashMap<String, String> hashMap = JSON.parseObject(json, HashMap.class);
        System.out.println(hashMap);
        Integer purchaseReportId = Integer.valueOf(Generator.getString(9));
        //往处理报表提交申请(只提交单号和生成申请号)
        String dealId = Generator.getString(9);
        for (String key :
                hashMap.keySet()) {
            System.out.println(key);
            String[] skey = key.split("&&");
            String barCode = skey[0];
            Integer supplierId = Integer.valueOf(skey[1]);
            Integer applyQuantity = Integer.valueOf(hashMap.get(key));

            String application = String.valueOf(getUserId());
            this.purchaseReportService.insertPurchaseReport(new PurchaseReport(String.valueOf(supplierId) ,String.valueOf(purchaseReportId) ,barCode,applyQuantity,application));
        }
        this.dealReportService.insertDealReport(new DealReport(dealId,purchaseReportId.toString()));
        return "success";
    }

    /**
     * 修改报表
     */
    @RequiresPermissions("system:purchasereport:edit")
    @GetMapping("/edit/{id1}")
    public String edit(@PathVariable("id1") String json, ModelMap mmap) {
        //这里不应该是根据reportId查询，应该是通过报表单号查询
//        PurchaseReport purchaseReport = purchaseReportService.selectPurchaseReportByReportId(reportId);
//        PurchaseReport purchaseReport = purchaseReportService.deletePurchaseReportByPurchaseReportId(reportId);

//        this.purchaseReportService.selectPurchaseReportList(new PurchaseReport())
//        mmap.put("purchaseReport", purchaseReport);
        String[] strings = json.split("==");
        String purchaseReportId=strings[0];
        String supplierId=strings[1];
        String barCode=strings[2];
        List<PurchaseReport> purchaseReports = this.purchaseReportService.selectPurchaseReportList(new PurchaseReport(supplierId, purchaseReportId, barCode));
        System.out.println(purchaseReportId+"----------"+supplierId+"==========="+barCode+"------------------");
        System.out.println(purchaseReports.get(0));
        mmap.put("purchaseReport",purchaseReports.get(0));
        return prefix + "/edit";
    }

    /**
     * 修改保存报表
     */
    @RequiresPermissions("system:purchasereport:edit")
    @Log(title = "报表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PurchaseReport purchaseReport) {
        return toAjax(purchaseReportService.updatePurchaseReport(purchaseReport));
    }

    /**
     * 删除报表
     */
    @RequiresPermissions("system:purchasereport:remove")
    @Log(title = "报表", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{id1}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id1") String json) {
        String[] strings = json.split("==");
        String purchaseReportId=strings[0];
        String supplierId=strings[1];
        String barCode=strings[2];
        return toAjax(purchaseReportService.deletePurchaseReportByPurchaseReport(new PurchaseReport(supplierId, purchaseReportId, barCode)));
    }
}
